import {
  getMenuList
} from '@/api';
import {
  dictList,
  apiProjectList,
  apiDictMoreList,
  apiPartitionList,
  apiFloornList,
  apiPaysonList,
  apiSystemList,
  apiProjectDetail,
  apiAssetCodeList,
  apiConstant
} from '../api/contract';

export default {
  data() {
    //0 草稿 1 审批中 2 审批完成/履行中 3 驳回 4 履行完毕 5 已废止
    return {
      statusList: [
        // {
        //   dictKey: '0',
        //   dictValue: '草稿'
        // }, 
        // {
        //   dictKey: 1,
        //   dictValue: '审批中'
        // },
        {
          dictKey: 2,
          dictValue: '履行中'
        },
        // {
        //   dictKey: 3,
        //   dictValue: '驳回'
        // },
        {
          dictKey: 4,
          dictValue: '履行完毕'
        },
        {
          dictKey: 5,
          dictValue: '已废止'
        }
      ],
      rightWrongList: [{
        dictKey: 1,
        dictValue: '是'
      }, {
        dictKey: 0,
        dictValue: '否'
      }],
      searchParams: {},
      spaceCodeList: [], //资产编号list
      spaceTypeList: [], //资产类型list
      pickerOptions: {
        disabledDate: time => {
          if (this.searchParams.contractBeginDate) {
            return time.getTime() < this.searchParams.contractBeginDate;
          }
        },
      },
      pickerOptionsStart: {
        disabledDate: time => {
          if (this.searchParams.contractEndDate) {
            return time.getTime() > this.searchParams.contractEndDate;
          }
        },
      },
      projectList: [],
      contractTypeList: [], //合同类型
      partitionList: [], //功能分区
      floorList: [], //楼层
      assetTypeList: [], //资产类型
      paySonList: [], //支出子类
      systemList: [], //设备系统
      systemSonList: [], //设备子系统
      businessTypeList: [], //业务类型
    }
  },
  methods: {
    //去详情 contractStatus: 0 草稿 1 审批中 2 审批完成/履行中 3 驳回 4 履行完毕 5 已废止
    async goPage(item, type) {
      let param = {
        parentId: type == 'first' ? this.$route.query.menuId : this.$route.query.tabId,
      };
      this.getHeaderParam(param, item, type);
      let res = await getMenuList(param);
      let menuUrl = res.rows[0].menuUrl;
      let separator = menuUrl.indexOf('?') > -1 ? '&' : '?';
      let queryData = {
        isRefresh: true,
        isInfo: type == 'draft' ? '' : true,
        parentMenuId: type == 'first' ? this.$route.query.menuId : this.$route.query.tabId,
        menuId: res.rows[0].primaryKey,
        contractKey: item.primaryKey ? item.primaryKey : '',
        contractCode: item.contractCode ? item.contractCode : '',
        contractType: item.contractType,
        headerName: "合同管理"
      };
      this.getHeaderParam(queryData, item, type);

      let paramData = [];
      for (let key in queryData) {
        paramData.push(key + '=' + queryData[key]);
      }

      let href = menuUrl + separator + paramData.join('&');
      window.open(href);
    },
    //区分导航传的参数 contractStatus: 0 草稿 1 审批中 2 审批完成/履行中 3 驳回 4 履行完毕 5 已废止
    getHeaderParam(param, item, type) {
      if (item.contractStatus == 0) {
        param.likeDescription = '0'; // 合同管理模块，合同查询模块的'草稿中'合同详情
      } else if (item.contractStatus == 1) {
        param.likeDescription = '2'; // 合同管理模块，合同查询模块的'审批中'合同详情
      } else if (item.contractStatus == 2 || item.contractStatus == 3 || item.contractStatus == 4 || item.contractStatus == 5) {
        if (type == "first") {
          if (item.contractType == 'ZX' || item.contractType == 'GC' || item.contractType == 'TG' || item.contractType == 'XZ' || item.contractType == 'CW' || item.contractType == 'YY') {
            param.likeDescription = '1'; //合同查询模块的执行中，审批完成，驳回，履行完毕，已废止'支出合同'详情
          } else {
            param.likeDescription = '3'; //合同查询模块的执行中，审批完成，驳回，履行完毕，已废止'租赁合同'详情
          }
        } else {
          if (item.contractType == 'ZX' || item.contractType == 'GC' || item.contractType == 'TG' || item.contractType == 'XZ' || item.contractType == 'CW' || item.contractType == 'YY') {
            param.likeDescription = '1a'; //合同管理模块的执行中，审批完成，驳回，履行完毕，已废止'支出合同'详情
          } else {
            param.likeDescription = '1b'; //合同管理模块的执行中，审批完成，驳回，履行完毕，已废止'租赁合同'详情
          }
        }

      }
    },

    //获取资产编号
    setAssetCodeList(type) {
      this.searchParams.spaceCodeList = []
      if (type == "assetType") {
        if (this.searchParams.projectKeys && this.searchParams.projectKeys.length > 0) {
          this.getAssetCodeList()
        }
      } else {
        this.getAssetCodeList()
      }
    },

    //字典
    async getOptionList() {
      let res = await apiDictMoreList({
        dictTypeList: ['contractType', 'space_type']
      });
      res.rows.forEach(item => {
        if (item.dictType == 'contractType') {
          this.contractTypeList = item.dictList || [];
        } else if (item.dictType == 'space_type') {
          this.assetTypeList = item.dictList || [];
        }
      })
    },
    //资产类型
    async getConstant() {
      let res = await apiConstant();
      this.spaceTypeList = res.spaceType || []
    },
    //功能分区
    async getPartitionList() {
      let projectKey = localStorage.getItem("projectKey")
      let res = await apiPartitionList({
        projectKey: this.$route.name == 'contract-query' ? projectKey : null
      })
      this.partitionList = res.rows || []
    },
    //楼层下拉
    async getFloornList() {
      let res = await apiFloornList({
        partitionKey: this.searchParams.spacePartition
      })
      res.rows = res.rows.sort((a, b) => {
        return a.buildingStorey < b.buildingStorey ? 1 : -1;
      })
      this.floorList = res.rows || []
    },
    //支出子类
    async getPaysonList() {
      let res = await apiPaysonList({
        bizType: 2
      })
      this.paySonList = res.rows || []
    },
    //项目详情
    async getProjectDetail(key) {
      let projectKey = key || localStorage.getItem("projectKey")
      let res = await apiProjectDetail({
        primaryKey: projectKey
      })
      this.getSystemList(res.spaceKey)
    },
    //资产codelist
    async getAssetCodeList() {
      let res = await apiAssetCodeList({
        projectKeys: this.searchParams.projectKeys || null,
        spaceTypeList: this.searchParams.assetType ? [this.searchParams.assetType] : null,
      })
      this.spaceCodeList = res.rows || []
    },
    //设备系统
    async getSystemList(key) {
      let res = await apiSystemList({
        spaceKey: key
      })
      this.systemList = res.rows || []
    },
    //设备子系统
    async getSonSystemList() {
      let res = await apiSystemList({
        parentPrimaryKey: this.searchParams.system
      })
      this.systemSonList = res.rows || []
    },
    getStatusName(value) {
      let [obj] = this.statusList.filter(item => item.dictKey == value);
      return obj ? obj.dictValue : '';
    },
    getContractName(value) {
      let [obj] = this.contractTypeList.filter(item => item.dictKey == value);
      return obj ? obj.dictValue : '';
    },
    //账单管理
    billManage() {
      this.$router.push({
        name: "bill-manage"
      })
    },
    //项目信息
    async getProjectList() {
      let res = await apiProjectList();
      this.projectList = res.rows || [];
    },
    //去验收单页面
    acceptanceManage() {
      let pathUrl = {
        name: 'acceptance-index'
      };
      let router = this.$router.resolve(pathUrl);
      window.open(router.href, '_blank');
    },
    //重制查询条件
    reset() {
      this.searchParams = {}
      this.getTableData('')
    }
  },
}
